#!groovy
pipeline {
    agent {
        node {
            label 'maven'
        }
    }
    //拉取代码
    stages {
        stage('clone code') {
            agent none
            steps {
                git(url: "${params.git_url}", credentialsId: "${params.gitCredentialsId}", branch: "${params.branch_name}", changelog: true, poll: false)
            }
        }

        //编译代码
        stage(' build') {
            agent none
            steps {
                container('maven') {
                    sh 'mvn clean package -DskipTests  -P test'
                }
            }
        }

        stage('SSH') {
            steps {
                script {
                    def remote = [:]
                    remote.name = 'test'
                    remote.host = '47.122.56.162'
                    remote.allowAnyHosts = true
                    withCredentials([usernamePassword(credentialsId: 'test', passwordVariable: 'password', usernameVariable: 'username')]) {
                        remote.user = "${username}"
                        remote.password = "${password}"
                    }
                    sshCommand remote: remote, command: "rm -f /root/ROOT.war"
                    sshPut remote: remote, from: './target/ROOT.war', into: '/root/ROOT.war'
                    sshCommand remote: remote, command: "systemctl stop tomcat"
                    sshCommand remote: remote, command: "rm -rf /www/server/tomcat9/webapps/ROOT"
                    sshCommand remote: remote, command: " unzip /root/ROOT.war -d /www/server/tomcat9/webapps/ROOT"
                    sshCommand remote: remote, command: "systemctl start tomcat"
                }
            }
        }
    }
    environment {
        //镜像仓库地址
        HARBOR_ADDRESS = "192.168.3.62:30002"
        //镜像仓库账号
        HARBOR_ACCOUNT = "admin"
        //镜像仓库名称
        HARBOR_PASSWORD = "Harbor12345"
        //镜像仓库项目名称
        HARBOR_PROJECT_NAME = "live"
        //发布项目名称
        APP_NAME = 'live-server'
        //dockerfile 位置
        DOCKERFILE_DIR = '.'
        //k8s yaml 文件
        K8S_YAML = 'k8s-dev.yaml'
    }
    parameters {
        string(name: 'project_name', defaultValue: 'live-server', description: '项目名称')
        string(name: 'git_url', defaultValue: 'http://192.168.3.58:3000/root/live.git', description: 'git仓库地址')
        string(name: 'gitCredentialsId', defaultValue: 'gitlab-id', description: 'git访问凭证')
        string(name: 'deploy_version', defaultValue: 'latest', description: '发布版本')
        string(name: 'branch_name', defaultValue: 'develop', description: 'git分支')
        string(name: 'profiles', defaultValue: 'prod', description: '环境')
        string(name: 'k8sServerUrl', defaultValue: 'https://192.168.3.62:6443', description: 'k8s集群服务地址')
    }
}





